package com.dingxin.portal.common.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class BatchReadFileForCsvUtil {
	private static final Log logger = LogFactory.getLog(BatchReadFileForCsvUtil.class);
	private static String splitChar=",";
	
	public static List<Map<String,String>> parseFileforAccount(File file, int startLine) {
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		
		try 
		{
			FileReader fReader = new FileReader(file);
	    	BufferedReader reader = new BufferedReader(fReader);
	    	String line = "";
	    	int lineNo = 1;
	    	for(int i = 0; i < startLine; i++)
	    	{
	    		line = reader.readLine();
	    		lineNo++;
	    	}
	    	String[] array = null;
	    	String stemp = "";
	    	String customerOrderId,logisticsId,logisticsCompany="",payerName="",payerPhone="",payerEmail="",desc="";
	    	
	    	while(!StringUtils.isBlank(line))
	    	{
	    		try
	    		{
		    		logger.info("line="+line);
		    		stemp = line.replaceAll("\"", "");
		    		array = stemp.split(splitChar);
		    		
		    		customerOrderId = array[0];
		    		logisticsId = array[1];
		    		if(array.length>2){
		    			logisticsCompany  =  array[2];
		    		}
		    		if(array.length>3){
		    			payerName  =  array[3];
		    		}
		    		if(array.length>4){
		    			payerPhone  =  array[4];
		    		}
		    		if(array.length>5){
		    			payerEmail  =  array[5];
		    		}
		    		if(array.length>6){
		    			desc =  array[6];
		    		}
		    		Map<String,String> map = new HashMap<String,String>();
					map.put("customerOrderId", customerOrderId);
					map.put("logisticsId", logisticsId);
					map.put("logisticsCompany", logisticsCompany);
					map.put("payerName", payerName);
					map.put("payerPhone", payerPhone);
					map.put("payerEmail", payerEmail);
					map.put("desc", desc);
					map.put("lineNo", "" + lineNo);
					list.add(map);
					line = reader.readLine();
					lineNo++;
	    		}
	    		catch(Exception e)
	    		{
	    			logger.error(e);
					e.printStackTrace();
					line = reader.readLine();
					lineNo++;
					continue;
	    		}
	    	}
		}
		catch(Exception e)
		{
			logger.error(e);
			e.printStackTrace();
		}
		return list;
	}
	public static List<String[]> parseFileToList(File file, int startLine){
		List<String[]> resultList = new ArrayList<String[]>();
		try 
		{
			FileReader fReader = new FileReader(file);
	    	BufferedReader reader = new BufferedReader(fReader);
	    	String line = "";
	    	int lineNo = 1;
	    	for(int i = 0; i < startLine; i++)
	    	{
	    		line = reader.readLine();
	    		lineNo++;
	    	}
	    	String stemp = "";
	    	
	    	while(!StringUtils.isBlank(line))
	    	{
	    		try
	    		{
		    		logger.info("line="+line);
		    		stemp = line.replaceAll("\"", "");
		    		String[] array  = stemp.split(splitChar);
		    		resultList.add(array);
					line = reader.readLine();
					lineNo++;
	    		}
	    		catch(Exception e)
	    		{
	    			logger.error(e);
					e.printStackTrace();
					line = reader.readLine();
					lineNo++;
					continue;
	    		}
	    	}
		}
		catch(Exception e)
		{
			logger.error(e);
			e.printStackTrace();
		}
		return resultList;
	}
}

